
//定义变量今年是2019年，5月份
var year = 2019;
var month = 5;


document.querySelector("#nian").innerHTML = year;
document.querySelector("#yue").innerHTML = month;

//计算从1980年到2018年一共多少天
var days = 0;

//计算是不是闰年
function runNian(y){
    if(y % 400 === 0 || (y % 4 === 0 && y % 100 != 0)){
        return true;
    }else{
        return false;
    }
}

//计算一共有多少天，闰年的话加366，其余的加上365
for(var i = 1980;i<year;i++){
    if(runNian(i)){
        days += 366;
    }else{
        days += 365;
    }
}


//计算2019年到4月份有多少天
for (var k =1;k < month;k++){
    switch(k){
        case 1:
            days += 31;
            break;
//      闰年的话2月份有29天，其余的加上28天
        case 2:
            if(runNian(year)){
                days += 29;
            }else{
                days += 28;
            }
            break;
        case 3:
            days += 31;
            break;
        case 4:
            days += 30;
            break;
        case 5:
            days += 31;
            break;
        case 6:
            days += 30;
            break;
        case 7:
            days += 31;
            break;
        case 8:
            days += 31;
            break;
        case 9:
            days += 30;
            break;
        case 10:
            days += 31;
            break;
        case 11:
            days += 30;
            break;
        case 12:
            days += 31;
            break;
    }
}

//算一下本月一共有多少天
var day = 0;

switch(month){
    case 1:
        day += 31;
        break;
    case 2:
        if(runNian(year)){
            day += 29;
        }else{
            day += 28;
        }
        break;
    case 3:
        day += 31;
        break;
    case 4:
        day += 30;
        break;
    case 5:
        day += 31;
        break;
    case 6:
        day += 30;
        break;
    case 7:
        day += 31;
        break;
    case 8:
        day += 31;
        break;
    case 9:
        day += 30;
        break;
    case 10:
        day += 31;
        break;
    case 11:
        day += 30;
        break;
    case 12:
        day += 31;
        break;
}
console.log(day);


//计算上个月一共有多少天
var prevDay = 0;

switch(month - 1){
    case 1:
        prevDay += 31;
        break;
    case 2:
        if(runNian(year)){
            prevDay += 29;
        }else{
            prevDay += 28;
        }
        break;
    case 3:
        prevDay += 31;
        break;
    case 4:
        prevDay += 30;
        break;
    case 5:
        prevDay += 31;
        break;
    case 6:
        prevDay += 30;
        break;
    case 7:
        prevDay += 31;
        break;
    case 8:
        prevDay += 31;
        break;
    case 9:
        prevDay += 30;
        break;
    case 10:
        prevDay += 31;
        break;
    case 11:
        prevDay += 30;
        break;
    case 12:
        prevDay += 31;
        break;
}
console.log(prevDay);


//因为1980年1月1日是星期二，为了让它从星期日开始，我们加上两天
days += 2;

console.log(days);


//计算本月的1号是星期几
var start = days % 7;
console.log(start);


//5月的日历界面一共有多少天  1周7天 * 6周 = 42天
var num = 42;


//下一个月有多少天  8天  一共有45天-上个月的3天-这个月的天数=下个月的天数
var nextDays = num - start - day;


//获取今天是几号
var thisdate = new Date().getDate();

var date1 = document.querySelector("#date1");


//填上上个月3天的空位
for (var j = prevDay - start + 1;j <= prevDay;j++){
    date1.innerHTML += "<span class='h'>" + j + "</span>";
}

//用for循环填上本月的天数
for (var a = 1;a <= day;a++){
    if(a == thisdate){
        date1.innerHTML += "<span class='today'>" + a + "</span>";
    }else{
        date1.innerHTML += "<span>" + a + "</span>";
    }
    
}

//用for循环补上下个月的8个空位
for (var s = 1;s <= nextDays;s++){
    date1.innerHTML  += "<span class='h'>" + s + "</span>";
}



